#include "napi/native_api.h"

#include <asm-generic/stat.h>
#include <bits/alltypes.h>
#include <fstream>
#include <stdexcept>
#include <string>

#include "LogUtils.h"
#include "program_cl.h"
#include "program_wrapper.h"

static napi_value Add(napi_env env, napi_callback_info info) {

    size_t requireArgc = 2;
    size_t argc = 2;
    napi_value args[2] = {nullptr};

    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);

    napi_valuetype valuetype1;
    napi_typeof(env, args[1], &valuetype1);

    double value0;
    napi_get_value_double(env, args[0], &value0);

    double value1;
    napi_get_value_double(env, args[1], &value1);

    napi_value sum;
    napi_create_double(env, value0 + value1, &sum);

    OH_LOG_INFO(LOG_APP, "Successfully visit %{private}f, reason:%{public}f.", value0, value1);

    return sum;
}

// 新增的 HelloWorld 函数
static napi_value HelloWorld(napi_env env, napi_callback_info info) {
    napi_value result;
    napi_create_string_utf8(env, "Hello World", NAPI_AUTO_LENGTH, &result);
    return result;
}
static napi_value GetNumber(napi_env env, napi_callback_info info) {
    napi_value result;
    napi_create_int32(env, 42, &result); // 返回整数 42
    return result;
}

static napi_value TestOpencl(napi_env env, napi_callback_info info) {
    OH_LOG_INFO(LOG_APP, "TestOpencl.");
    napi_value undefined;
    napi_get_undefined(env, &undefined);
    //     ProgramCl programCl;
    //     programCl.runMethod();

    //     ProgramWrapper programWrapper;
    //     programWrapper.runMethod();

    return undefined;
}

ProgramCl programCl;
//     programCl.runMethod();
static napi_value ClMethodPath(napi_env env, napi_callback_info info) {
    size_t argc = 1;
    napi_value args[1];
    napi_value thisArg;
    void *data;
    // 1. 获取参数
    napi_status status = napi_get_cb_info(env, info, &argc, args, &thisArg, &data);
    if (status != napi_ok || argc < 1) {
        napi_throw_error(env, NULL, "Invalid arguments");
        return nullptr;
    }
    // 2. 检查参数类型是否为字符串
    napi_valuetype valuetype;
    napi_typeof(env, args[0], &valuetype);
    if (valuetype != napi_string) {
        napi_throw_type_error(env, NULL, "Expected string argument");
        return nullptr;
    }

    // 3. 获取字符串内容
    size_t str_length;
    napi_get_value_string_utf8(env, args[0], NULL, 0, &str_length);

    char *path = (char *)malloc(str_length + 1);
    size_t copied;
    napi_get_value_string_utf8(env, args[0], path, str_length + 1, &copied);
    OH_LOG_DEBUG(LOG_APP, "Received path: %s", path);

    programCl.setFilePath(path);
    programCl.runMethod();

    napi_value result;
    napi_create_string_utf8(env, path, str_length, &result);
    // 5. 释放内存
    free(path);

    return result;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {
    napi_property_descriptor desc[] = {
        {"add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"helloWorld", nullptr, HelloWorld, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"getNumber", nullptr, GetNumber, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"testOpencl", nullptr, TestOpencl, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"clMethodPath", nullptr, ClMethodPath, nullptr, nullptr, nullptr, napi_default, nullptr},
    };
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "entry",
    .nm_priv = ((void *)0),
    .reserved = {0},
};

extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
